#include "serverHead/tok.h"

int makeTok(train_t *train){
    char* jwt;
    size_t jwt_length;

    struct l8w8jwt_encoding_params params;
    l8w8jwt_encoding_params_init(&params);

    params.alg = L8W8JWT_ALG_HS512;//算法

    params.sub = "Yunpan";//主题
    params.iss = "Loalad";//签发人
    params.aud = "Username";//受众

    params.iat = 0;//签发时间
    params.exp = 0x7fffffff; /* 过期时间Set to expire after 10 minutes (600 seconds). */


    params.secret_key = (unsigned char*)train->name;//把用户的名字放在这里生成jwt
    params.secret_key_length = strlen(params.secret_key);

    params.out = &jwt;
    params.out_length = &jwt_length;

    int r = l8w8jwt_encode(&params);    
    strcpy(train->tok, jwt);
    l8w8jwt_free(jwt);
}

int verifyTok(train_t *train){
    char* jwt;
    size_t jwt_length;

    struct l8w8jwt_encoding_params params;
    l8w8jwt_encoding_params_init(&params);

    params.alg = L8W8JWT_ALG_HS512;//算法
    params.sub = "Yunpan";//主题
    params.iss = "Loalad";//签发人
    params.aud = "Username";//受众

    params.iat = 0;//签发时间
    params.exp = 0x7fffffff; /* 过期时间Set to expire after 10 minutes (600 seconds). */


    params.secret_key = (unsigned char*)train->name;//把用户的名字放在这里生成jwt
    params.secret_key_length = strlen(params.secret_key);

    params.out = &jwt;
    params.out_length = &jwt_length;

    int r = l8w8jwt_encode(&params);
    
    int ret = strcmp(train->tok, jwt);//验证通过返回0
    l8w8jwt_free(jwt);
    return ret;
}

